package com.tan.firstblog.dao.implement;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.tan.firstblog.dao.interfaces.PostDao;
import com.tan.firstblog.models.Post;

@Repository
public class PostDaoImpl implements PostDao {

	private static final Logger logger = LoggerFactory.getLogger(PostDaoImpl.class); 
	
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}

	
	@SuppressWarnings("unchecked")
	public List<Post> getAllPosts() {
		List<Post> listPost = null;
		try {
			Session session = this.getSession();
			listPost = session.createQuery("from Post").list();
		} catch(Exception e) {
			logger.info(e.toString());
		}
		return listPost;
	}
}
